#include "zigbeenodetreewidget.h"
#include "ui_zigbeenodetreewidget.h"

#include <QHBoxLayout>
#include <QTreeWidgetItem>

ZigBeeNodeTreeWidget::ZigBeeNodeTreeWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ZigBeeNodeTreeWidget)
{
    ui->setupUi(this);
    initUI();
    initTree();
    connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),
            this,SLOT(treeItemChange(QTreeWidgetItem*,int)));
    //    connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),
    //            this,SLOT(showMessage(QTreeWidgetItem*,int)));
    //    connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),
    //            this,SLOT(creatCheckStatusNodes(QTreeWidgetItem*,int)));
}

ZigBeeNodeTreeWidget::~ZigBeeNodeTreeWidget()
{
    delete ui;
}
void ZigBeeNodeTreeWidget::initUI()
{
    //    mainLayout = new QHBoxLayout;
    //    mainLayout->addWidget(ui->treeWidget);

    //    setLayout(mainLayout);
}
void ZigBeeNodeTreeWidget::initTree()
{
    ui->treeWidget->clear();

    allNode = new QTreeWidgetItem(ui->treeWidget);
    allNode->setText(0,trUtf8("所有节点"));
    allNode->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    allNode->setCheckState(0,Qt::Unchecked);

    activeNode = new QTreeWidgetItem(allNode);
    activeNode->setText(0,trUtf8("活动节点"));
    activeNode->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    activeNode->setCheckState(0,Qt::Unchecked);

    unactiveNode = new QTreeWidgetItem(allNode);
    unactiveNode->setText(0,trUtf8("不活动节点"));
    unactiveNode->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    unactiveNode->setCheckState(0,Qt::Unchecked);
}
void ZigBeeNodeTreeWidget::showMessage(QTreeWidgetItem *item, int c)
{
    qDebug()<<item->text(0)<<'\n'<<c;
}
void ZigBeeNodeTreeWidget::treeItemChange(QTreeWidgetItem *item, int c)
{
    //不活动的节点不处理
    if (unactiveNodeList.contains(item) || item == unactiveNode) {
        return;
    }
    if (Qt::Checked == item->checkState(c)) {
        int childCount = item->childCount();
        if (childCount > 0) {
            for (int i = 0;i < childCount;++i) {
                item->child(i)->setCheckState(c,Qt::Checked);
            }
            emit sig_activeNode(ALLNODE);
        } else {
            //   if (item != unactiveNode && item != activeNode) {
            // emit sig_activeNode(item->text(0));
            // setUncheck(item,c);
            emit sig_activeNode("NOTALL");
            emit sig_activeNode(item->text(0).toUShort());
            //  }
        }

    }
    else if (Qt::Unchecked == item->checkState(c)) {
        int childCount = item->childCount();
        if (childCount > 0) {
            for (int i = 0;i < childCount;++i) {
                item->child(i)->setCheckState(c,Qt::Unchecked);
            }
        }
    }
}
void ZigBeeNodeTreeWidget::addNode(const ZigBeeTreeNode &node)
{
    //应该防止插入相同的节点
    //  addNodeInlist(node);
    switch (node.status) {//在视图中插入不同的位置
    case ACTIVE:insertNode(node.devName,activeNode);break;//插到活动树节点下
    case UNACTIVE:insertNode(node.devName,unactiveNode);break;//插入中不活动树节点下
    default:break;
    }
}
/*
 *note:先检测如果是新的节点则加入到节点中，不是则根据现在的状态和传来的状态做相关的活动
 *
 *
 *
 *
 *
 */
void ZigBeeNodeTreeWidget::slt_nodeChange(ushort name, bool states)
{
    //    if (!isExist(adress)) {

    //    }

    ZigBeeTreeNode node_t;
    node_t.devName = name;
    node_t.status = states;
    addNodeInlist(node_t);

    if (states) {//成为活动节点
        beActive(node_t);

    } else {//非活动节点
        beUnActive(node_t);

    }

}
/*
 *param: parent插入节点的父节点
 *
 */
void ZigBeeNodeTreeWidget::insertNode(ushort devname, QTreeWidgetItem *parent)//插入一个节点
{
    QTreeWidgetItem *item = new QTreeWidgetItem(parent);
    item->setText(0,QString::number(devname));
    item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    item->setCheckState(0,Qt::Unchecked);
    if (parent == activeNode) {
        activeNodeList.append(item);
    }
    else {
        unactiveNodeList.append(item);
    }
}
void ZigBeeNodeTreeWidget::deleteNode(const ZigBeeTreeNode &node)//删除一个节点
{
    //        QList<QTreeWidgetItem*> list = ui->treeWidget->findItems(node.devName,Qt::MatchExactly,0);
    //        foreach(const QTreeWidgetItem *temp,list)
    //        {
    //            ui->treeWidget->removeRow(ui->treeWidget->row(temp));
    //        }
    //从两个链表中删除，确保删除
    if (!removeNode(node.devName,activeNodeList)) {
        removeNode(node.devName,unactiveNodeList);
    }



}
void ZigBeeNodeTreeWidget::moveNode(const ZigBeeTreeNode &node)//移动一个节点，从不动到动或相反
{
    deleteNode(node);
    addNode(node);
}
/**
 * @brief ZigBeeNodeTreeWidget::removeNode
 * @param devname
 * @param list
 * @return
 *
 * 这边抛出段错误
 */
bool ZigBeeNodeTreeWidget::removeNode(ushort devname, QList<QTreeWidgetItem *> &list)//去除一个树节点项
{
    QList<QTreeWidgetItem*>::iterator it;
    for (it = list.begin();it != list.end();++it) {
        if ((*it)->text(0) == QString::number(devname)) {
            delete *it;
            list.removeOne((*it));

            //            list.removeOne((*it)); 原来写的，为戒
            //            delete *it;
            return true;
        }
        //        else {
        //            return false;
        //        }
    }
    return false;
}
void ZigBeeNodeTreeWidget::creatCheckStatusNodes(QTreeWidgetItem *item, int c)
{
    StringIntPair temp(item->text(c),item->checkState(c));
    QList<StringIntPair>::iterator it;
    for (it = checkStatusNodes.begin();it != checkStatusNodes.end();++it) {
        if ((*it).first == temp.first) {
            (*it).second = temp.second;
            return;
        }
    }
    checkStatusNodes.append(temp);
}
bool ZigBeeNodeTreeWidget::hadHave(ushort adress)//判断地址是否在节点在中
{
    QList<QTreeWidgetItem*>::iterator it = activeNodeList.begin();
    for (;it != activeNodeList.end();++it) {
        if ((*it)->text(0).toUShort() == adress) {
            return true;
        }
    }
    QList<QTreeWidgetItem*>::iterator it2 = activeNodeList.begin();
    for (;it2 != activeNodeList.end();++it2) {
        if ((*it2)->text(0).toUShort() == adress) {
            return true;
        }
    }
    return false;

}
void ZigBeeNodeTreeWidget::beActive(const ZigBeeTreeNode &adress)//使地址为adress的节点成为活动节点
{
    moveNode(adress);

}
void ZigBeeNodeTreeWidget::beUnActive(const ZigBeeTreeNode &adress)//使地址为adress的节点成为bu活动节点
{
    moveNode(adress);
}
bool ZigBeeNodeTreeWidget::isExist(ushort node)//在节点链表中查找是否存在这个节点
{
    QList<ZigBeeTreeNode>::iterator it = list_Node.begin();
    for (;it != list_Node.end();++it) {
        if ((*it).devName == node) {
            return true;
        }
    }
    return false;

}
void ZigBeeNodeTreeWidget::addNodeInlist(const ZigBeeTreeNode &node)//插入一个节点，如果存在则更新状态，没有则插入
{
    QList<ZigBeeTreeNode>::iterator it = list_Node.begin();
    for (;it != list_Node.end();++it) {
        if ((*it).devName == node.devName) {
            (*it).status = node.status;
            return;
        }
    }
    list_Node.append(node);
}
//ZigBeeTreeNode& ZigBeeNodeTreeWidget::getNode(const QString &adress)//在节点链表中查找
//{
//    QList<ZigBeeTreeNode>::iterator it = list_Node.begin();
//    for (;it != list_Node.end();++it) {
//        if ((*it).devName == adress) {
//            return (*it);
//        }
//    }

//}
void ZigBeeNodeTreeWidget::setUncheck(QTreeWidgetItem *item,int c)
{
    foreach (QTreeWidgetItem *i, activeNodeList) {
        if (i != item) {
            i->setCheckState(c,Qt::Unchecked);
        }
    }

}

void ZigBeeNodeTreeWidget::clear()
{
    activeNodeList.clear();
    unactiveNodeList.clear();
    list_Node.clear();
    initTree();

}



